package com.hqyj.controller;

import com.hqyj.dao.InfoDao;
import com.hqyj.dao.UserInfoDao;
import com.hqyj.pojo.Info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;


import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 处理 登录请求的 控制器类
 */
@Controller //表示这个类 处理 前端页面的请求
public class LoginController {
    // 创建现实接口UserInfoDao的实现类对象
    @Autowired(required = false)
    UserInfoDao dao;

    //访问login页面
    @RequestMapping("/login")
    public String login() {

        return "login";
    }

    //    处理登录按钮提交的请求
    @RequestMapping("/loginForm")
    public String loginForm(String userName, String userPwd, Model model) {

        //查询数据，获取返回值Search_username
        int num1 = dao.Search_username(userName); //查询用户名
        int num2 = dao.Search_userPwd(userPwd); //查询密码

        //若用户名名和密码是在数据库中存在 则给用户提示登录成功
        //若查询结果大于0，则成功登录，否则失败
        if (num1 > 0) {
            if (num2 > 0) {
                System.out.println("登录成功");
                //定义要传给前端的提示信息
                model.addAttribute("info", "登陆成功");
                //去到主页面
                return "index";
            } else {
                model.addAttribute("info", "登陆失败 你输入的密码有误");
                System.out.println("登陆失败 你输入的密码有误");
                //返回登录页面
                return "login";
            }

        } else {
            model.addAttribute("info", "登陆失败 你输入的用户名有误");
            System.out.println("登陆失败 你输入的用户名有误");
            //返回登录页面
            return "login";
        }
    }


    //访问index页面
    @RequestMapping("/index")
    public String index() {

        return "index";
    }

    //访问chaxun页面
    @RequestMapping("/chaxun")
    public String chaxun() {

        return "chaxun";
    }

    //访问welcome页面
    @RequestMapping("/welcome")
    public String welcome() {

        return "welcome";
    }

    //访问update页面
    @RequestMapping("/update")
    public String update() {

        return "update";
    }

    //访问注册页面
    @RequestMapping("/zhuce")
    public String zhuce() {

        return "zhuce";
    }

    //    处理注册按钮提交的请求
    @RequestMapping("/zhuceForm")
    public String zhuceForm(String userName, String userPwd, String email, Model model) {

        //验证重名
        int num1 = dao.Search_username(userName);
        int num2 = dao.Search_email(email);


        if (num1 <= 0) {
            if (num2 <= 0) {
                int len = userPwd.length();
                if (len > 6) {
                    int num = dao.zhuce(userName, userPwd, email);
                    if (num > 0) {
                        //要传给前端的提示信息
                        model.addAttribute("info", "注册成功");
                        System.out.println("注册成功");
                        return "login";
                    } else {
                        model.addAttribute("info", "注册失败");
                        System.out.println("注册失败");
                        return "zhuce";
                    }
                } else {
                    model.addAttribute("info", "注册失败 密码长度需大于6");
                    System.out.println("注册失败 密码长度需大于6");
                    return "zhuce";
                }
            } else {
                model.addAttribute("info", "注册失败 该邮箱已存在");
                System.out.println("注册失败 该邮箱已存在");
                return "zhuce";
            }
        } else {
            model.addAttribute("info", "注册失败 该用户名已存在");
            System.out.println("注册失败 已存在");
            return "zhuce";
        }


    }


    @Autowired(required = false)

//    @Autowired(required = false)
            InfoDao infoDao;

    @RequestMapping("/welcomeAjax")
    @ResponseBody
    private HashMap welcomeAjax() {
        HashMap<String, Object> map = new HashMap();


        List<Info> infoList = infoDao.selectMap();

        List<HashMap<String, Object>> listMap = new ArrayList<>();

        //遍历infoList
        for (int i = 0; i < infoList.size(); i++) {
            HashMap<String, Object> m = new HashMap<>();
            m.put("name", infoList.get(i).getProvinceName());
            m.put("value", infoList.get(i).getDeadCount());

            listMap.add(m);
        }


        //传给前端
        map.put("welcome", listMap);

        return map;

    }

    @RequestMapping("/bingAjax")
    @ResponseBody
    public HashMap bingAjax() {
        HashMap<String, Object> map = new HashMap();

        List<Info> infoList = infoDao.selectBing();

        List<HashMap<String, Object>> listBing = new ArrayList<>();

        //遍历infoList
        for (int i = 0; i < infoList.size(); i++) {
            HashMap<String, Object> m = new HashMap<>();
            m.put("name", infoList.get(i).getProvinceName());
            m.put("value", infoList.get(i).getConfirmCount());

            listBing.add(m);
        }


        //传给前端
        map.put("bing", listBing);

        return map;

    }

    @RequestMapping("/zhuAjax")
    @ResponseBody
    public HashMap zhuAjax() {
        HashMap<String, Object> map = new HashMap();

        List<Info> infoList = infoDao.selectZhu();

        List<String> listX = new ArrayList<>();
        //创建Y轴的数据集合
        List<Integer> listY = new ArrayList<>();

        //遍历infoList
        for (int i = 0; i < infoList.size(); i++) {
            //添加X数据
            listX.add(infoList.get(i).getProvinceName());
            //添加Y数据
            listY.add(infoList.get(i).getCuredCount());
        }


        //传给前端
        map.put("x", listX);
        map.put("y", listY);

        return map;

    }

    @RequestMapping("/paiAjax")
    @ResponseBody
    public HashMap paiAjax() {
        HashMap<String, Object> map = new HashMap();

        List<Info> infoList = infoDao.selectPai();

        List<Integer> listX = new ArrayList<>();
        //创建Y轴的数据集合
        List<String> listY = new ArrayList<>();

        //遍历infoList
        for (int i = 0; i < infoList.size(); i++) {
            //添加X数据
            listX.add(infoList.get(i).getDeadCount());
            //添加Y数据
            listY.add(infoList.get(i).getProvinceName());
        }


        //传给前端
        map.put("x", listX);
        map.put("y", listY);

        return map;

    }

    @RequestMapping("/lastAjax")
    @ResponseBody
    public HashMap lastAjax() {
        HashMap<String, Object> map = new HashMap();

        String xx = infoDao.selectLast();

        map.put("x", xx);
        return map;

    }

    @RequestMapping("/fastAjax")
    @ResponseBody
    public HashMap fastAjax() {
        HashMap<String, Object> map = new HashMap();

        String xx = infoDao.selectFast();

        map.put("x", xx);
        return map;

    }

    @RequestMapping("/zhanAjax")
    @ResponseBody
    public HashMap zhanAjax(String provinceName) {
        HashMap<String, Object> map = new HashMap();

        List<Info> infoList = infoDao.selectZhan(provinceName);

        List<HashMap<String, Object>> listZhan = new ArrayList<>();

        //遍历infoList
        for (int i = 0; i < infoList.size(); i++) {
            HashMap<String, Object> m1 = new HashMap<>();
            m1.put("name", "确诊");
            m1.put("value", infoList.get(i).getConfirmCount());

            HashMap<String, Object> m2 = new HashMap<>();
            m2.put("name", "治愈");
            m2.put("value", infoList.get(i).getCuredCount());

            HashMap<String, Object> m3 = new HashMap<>();
            m3.put("name", "死亡");
            m3.put("value", infoList.get(i).getDeadCount());

            listZhan.add(m1);
            listZhan.add(m2);
            listZhan.add(m3);
        }


        //传给前端
        map.put("zhan", listZhan);

        return map;

    }

    @RequestMapping("/shenAjax")
    @ResponseBody
    public HashMap shenAjax(String areaName) {
        HashMap<String, Object> map = new HashMap();

        List<Info> infoList = infoDao.selectShen(areaName);

        List<HashMap<String, Object>> listShen = new ArrayList<>();

        //遍历infoList
        for (int i = 0; i < infoList.size(); i++) {
            HashMap<String, Object> m1 = new HashMap<>();
            m1.put("name", "确诊");
            m1.put("value", infoList.get(i).getConfirmCount());

            HashMap<String, Object> m2 = new HashMap<>();
            m2.put("name", "治愈");
            m2.put("value", infoList.get(i).getCuredCount());

            HashMap<String, Object> m3 = new HashMap<>();
            m3.put("name", "死亡");
            m3.put("value", infoList.get(i).getDeadCount());

            listShen.add(m1);
            listShen.add(m2);
            listShen.add(m3);
        }


        //传给前端
        map.put("shen", listShen);

        return map;

    }

    @RequestMapping("/chaAjax")
    @ResponseBody
    public HashMap chaAjax(String provinceName) {
        HashMap<String, Object> map = new HashMap();

        List<String> infoList = infoDao.selectCha(provinceName);


        //传给前端
        map.put("cha", infoList);

        return map;

    }
}
